package noncomunicating.classes.SSI;

import java.io.FileNotFoundException;

import comunicating.classes.ServerLoop;

import util.FileToBigException;
import util.IO;
import util.Regex;

public class Include extends Directive{
	private String incURL;
	
	private void setIncURL() throws SSIException{
		if(cmt.contains("virtual")){
			incURL = ServerLoop.directory + "/" + incURL;
		}
		else if(cmt.contains("file")){
			incURL = ServerLoop.directory + "/" + this.getCallDirectory() + incURL;
		}
		else{
			throw new SSIException("Invalid Tag");
		}
	}
	
	public Include(String cmt, SSIParser parser){
		super(cmt, parser);
		incURL = Regex.firstMatch("[a-zA-Z0-9]+\\.[a-zA-Z0-9]+", cmt);
		
		try {
			setIncURL();
			String sub = IO.getFileString(incURL);
			this.subTag(cmt, sub);
		} catch (SSIException e) {
			this.subTag(cmt, e.tag);
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			this.subTag(cmt, "<!-- File Not Found -->");
		} catch (FileToBigException e) {
			e.printStackTrace();
		}
		
	}

}
